package com.ddd.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.UnknownHostException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;

public class ConvertToJSP {

    public static void main(String[] args) throws SQLException, UnknownHostException {
        new ConvertToJSP().doIt();

    }

    private void doIt() throws SQLException {
        String[] ext = {"html", "htm"};
        Collection<File> found = FileUtils.listFiles(new File("C:\\Documents and Settings\\eric\\桌面\\專題網頁\\"), ext, true);

        for (File f : found) {
            try {
                String path = f.getParent();
                String newName = path + File.separator + Util.getFilenameWithoutExtention(f.getAbsolutePath()) + ".jsp";
            
                BufferedReader br = Util.getBufferedReader(f.getAbsolutePath(), "utf-8");
                PrintWriter pw  = new PrintWriter(new FileWriter(newName));

                String str = null;
                
                pw.println("<%@page contentType=\"text/html\" pageEncoding=\"UTF-8\"%>");
                
               while ((str = br.readLine()) != null){
                   str = str.replaceAll("html", "jsp");
                   pw.println(str);
               }
               pw.flush();
               pw.close();
               br.close();
               
               f.deleteOnExit();
               
               System.out.println(newName);
            } catch (IOException ex) {
                Logger.getLogger(ConvertToJSP.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
